package sy.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.db.mysql.dao.CDGLDao;
import sy.db.mysql.dao.TresourceDao;
import sy.db.mysql.dao.TroleDao;
import sy.db.mysql.dao.TroleTresourceDao;
import sy.db.mysql.model.CDGL;
import sy.db.mysql.model.Tresource;
import sy.db.mysql.model.TroleTresource;
import sy.pageModel.DataGrid;
import sy.pageModel.SessionInfo;
import sy.pageModel.Tree;
import sy.services.CDGLServiceI;


@Service("cDGLService")
public class  CDGLServiceImpl implements  CDGLServiceI {
	
	@Autowired
	 CDGLDao  cDGLDao;
	@Override
	public List<Tree> tree(SessionInfo sessionInfo ,int deep) {
		List<Tree> lt = new ArrayList<Tree>();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("JIBIE_EQ", "1");
		params.put("order", "DESC");
		List<Map> l = cDGLDao.findResourceByUserIDByGrid(params);
		if (l != null && l.size() > 0) {
			for (int i=0;i<l.size();i++) {
				Map r = l.get(i);
				Tree tree = new Tree();
				tree.setId(r.get("ID").toString());
				tree.setText(r.get("NAME").toString());
				Map<String, Object> attr = new HashMap<String, Object>();
				tree.setAttributes(attr);
				Map<String, Object> params2 = new HashMap<String, Object>();
				params2.put("CD_ID", r.get("ID"));// 菜单类型的资源
				List<Map> l2 = cDGLDao.findResourceByUser2IDByGrid(params2);
				if(l2==null || l2.size() ==0){
					continue;
				}
				List<Tree> children = new ArrayList<Tree>();
				tree.setChildren(children);
				if(l2!=null && l2.size()>0){
					for(Map rr:l2){
						Tree child = new Tree();
						child.setId(rr.get("CD_ID").toString());
						child.setText(rr.get("NAME").toString());
						Map<String, Object> attr2 = new HashMap<String, Object>();
						attr2.put("url", rr.get("URL").toString());
						child.setAttributes(attr2);
						children.add(child);
						if(deep ==3 ){
							Map<String, Object> params3 = new HashMap<String, Object>();
							params3.put("PIDSTR_EQ", rr.get("CD_ID"));// 菜单类型的资源
							params3.put("order", "ASC");
							if (sessionInfo != null) {
								params2.put("TUSERIDSTR_EQ", sessionInfo.getId());// 自查自己有权限的资源
							}
							List<Map> l3 = cDGLDao.findResourceByUser2IDByGrid(params3);
							List<Tree> children2 = new ArrayList<Tree>();
							child.setChildren(children2);
							for(Map rrr:l3){
								try {
									Tree child2 = new Tree();
									child2.setId(rrr.get("ID").toString());
									child2.setText(rrr.get("NAMESTR").toString());
									child2.setIconCls(rrr.get("ICONSTR").toString());
									Map<String, Object> attr22= new HashMap<String, Object>();
									attr22.put("url", rrr.get("URLSTR"));
									child2.setAttributes(attr22);
									children2.add(child2);
								} catch (Exception e) {
								}
							}
						}
					}
				}
				lt.add(tree);
			}
		}
		return lt;
	}
	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int insert(CDGL record) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public CDGL selectByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int updateByPrimaryKeySelective(CDGL record) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public DataGrid findbyByGrid(Map map) {
		// TODO Auto-generated method stub
		return null;
	}
}
